const electron = require('electron')
// Module to control application life.
const {app,BrowserWindow,nativeImage,ipcMain} = electron;

const path = require('path-extra');

global.ROOT = __dirname;
global.APPDATA_PATH = path.join(app.getPath('appData'), 'Habitica-PC');
global.EXROOT = global.APPDATA_PATH;

const config = require('./lib/config');

const ref = require('./lib/utils');

disableHA = config.get('habitica.disableHA', false);

if (disableHA) {
  app.disableHardwareAcceleration();
}

app.setAppUserModelId('com.huaji.habitica');

if (process.platform === 'win32' && config.get('habitica.createShortcut', true)) {
  windowsShortcuts = require('windows-shortcuts-appid');
  shortcutPath = app.getPath('appData') + "\\Microsoft\\Windows\\Start Menu\\Programs\\Habitica.lnk";
  targetPath = app.getPath('exe');
  argPath = app.getAppPath();
  try {
    fs.accessSync(shortcutPath);
    windowsShortcuts.edit(shortcutPath, {
      target: targetPath,
      args: argPath
    }, function() {
      return windowsShortcuts.addAppId(shortcutPath, 'com.huaji.habitica');
    });
  } catch (error1) {
    error = error1;
    try {
      windowsShortcuts.create(shortcutPath, {
        target: targetPath,
        args: argPath
      }, function() {
        return windowsShortcuts.addAppId(shortcutPath, 'com.huaji.habitica');
      });
    } catch (undefined) {}
  }
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

let mainWindowDisplay

global.mainWindow = mainWindow;

function createWindow () {
  mainWindowDisplay = config.get('habitica.window', {width:800,height:600,isFullScreen:false,isMaximized:false})
  // Create the browser window.
  mainWindow = new BrowserWindow({
    transparent: true,
    width: mainWindowDisplay.width,
    height: mainWindowDisplay.height,
    frame: false,
    titleBarStyle: 'hidden',
    icon:"/img/logo.ico"
  })

  if (process.platform === 'darwin') {
    if (/electron$/i.test(process.argv[0])) {
      icon = nativeImage.createFromPath(ROOT + "/img/habitica.png");
      if ((ref3 = app.dock) != null) {
        if (typeof ref3.setIcon === "function") {
          ref3.setIcon(icon);
        }
      }
    }
  } else {
    mainWindow.setMenu(null);
  }

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/index.html`)

  // Open the DevTools.
  if(config.get('habitica.openDevTools', false)){
    mainWindow.webContents.openDevTools()
  }

  if (mainWindowDisplay.isMaximized) {
    mainWindow.maximize();
  }
  if (mainWindowDisplay.isFullScreen) {
    mainWindow.setFullScreen(true);
  }

  mainWindow.on('resize',function(){
    mainWindowDisplay['width'] = mainWindow.getSize()[0];
    mainWindowDisplay['height'] = mainWindow.getSize()[1];
    mainWindowDisplay['isFullScreen'] = mainWindow.isFullScreen();
    mainWindowDisplay['isMaximized'] = mainWindow.isMaximized();
  })

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    try{
    config.set('habitica.window',mainWindowDisplay);
    mainWindow = null
    }catch(e){
      console.log(e);
    }
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

ipcMain.on('getMainWindowsId',(event)=>{
  event.returnValue = mainWindow.id
})

ipcMain.on('relaunch',(event)=>{
  console.log(123);
  app.exit(0)
  app.relaunch();
})